package ru.arriah.servicedesk.web.leadExecutor.action;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.RequestBean;
import ru.arriah.servicedesk.bean.TaskBean;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;
import ru.arriah.servicedesk.web.commonaction.exception.PrivilegesException;

public class RequestDetailsAction extends DumbAction {
   protected RequestManagerLocalHome requestManagerHome;   
   
   public RequestDetailsAction() throws NamingException {
      super();

      requestManagerHome =
         Utils.getRequestManagerHomeInterface();     
   }
   
   
   /**
    * Constructor for manual action execution
    * @param requestManagerHome
    * @throws NamingException
    */
   public RequestDetailsAction(
      RequestManagerLocalHome requestManagerHome,
      Properties config
   ) throws NamingException {
     
      super();
      setConfig(config);
      this.requestManagerHome = requestManagerHome;
      
   }

   
   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
     try {
       
       int leadExecutorId = Utils.getUserId(request);
       int requestId = idVal(request.getParameter("request_id"));
       
       RequestManagerLocalObject requestManager = 
         requestManagerHome.create();
       
       
       // select request details
       RequestBean requestBean = 
         requestManager.selectRequestDetails(requestId);
       
       Collection<TaskBean> tasksList = 
          requestManager.selectTasksByRequest(
             requestId,
             "executor",
             "ASC"
          );
       
       Iterator i = tasksList.iterator();
       boolean permission = false;
       
       while (i.hasNext() && !permission) {
          int id = ((TaskBean)i.next()).getEmployeeId();
          if (id == leadExecutorId) permission = true;              
       }
       
       
       // control access privileges
       if (requestBean.getLeadExecutorId() == leadExecutorId)
          request.setAttribute("status", "request_lead_executor");
       else if (permission) {
          request.setAttribute("status", "task_executor");
       } else
          throw new PrivilegesException();                        
       
       requestManager.remove();
       
       request.setAttribute("requestBean", requestBean);
       request.setAttribute("tasksList", tasksList);                                                       
       
       return actionMapping.getSuccessTarget();       
    } catch (Exception e) {
       handleException(e, "execute");
       throw new InternalException("ErrorMessage.internal");
    }
   }
}